package com.itheima.controller;


import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Students;
import com.itheima.service.StudentsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
@Tag(name = "学生模块", description = "学生相关接口")
public class StudentController {

    @Autowired
    private StudentsService studentsService;

    // 查询所有学生
    // 分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date birth) {
        log.info("查询所有学生, page:{}, pageSize:{}, name:{}, gender:{}, birth:{}", page, pageSize, name, gender, birth);
        PageBean pageBean = studentsService.page(page, pageSize, name, gender, birth);
        return Result.success(pageBean);
    }


    // 批量删除工作
    @DeleteMapping("/{studentids}")
    public Result delete(@PathVariable List<Integer> studentids) {
        log.info("批量删除操作, studentids:{}", studentids);
        studentsService.delete(studentids);
        return Result.success();
    }

    // 新增学生
    @PostMapping
    public Result insert(@RequestBody Students student) {
        log.info("新增学生, student:{}", student);
        studentsService.insert(student);
        return Result.success();
    }

    // 根据studentid查询学生
    @GetMapping("/{studentid}")
    public Result getById(@PathVariable Integer studentid) {
        log.info("根据studentid查询学生, studentid:{}", studentid);
        Students students = studentsService.getById(studentid);
        return Result.success(students);
    }

    // 更新学生信息
    @PutMapping
    public Result update(@RequestBody Students student) {
        log.info("更新学生信息, student:{}", student);
        studentsService.update(student);
        return Result.success();
    }
}
